/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reminder;

import abstractmodel.*;
import dataaccess.IDataSource;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

/**
 *
 * @author littlechild
 */
public class RemainReminder implements ReminderObject {
    public static final int WINDOW_REMIND=0;
    public static final int EMAIL_REMIND=1;
    String username;
    int account_id;
    double money;
    int mode;
    String remindingTile="smallpie.iWallet remain reminder";
    String remindingBody;
    public RemainReminder(String username,int account_id) {
        this.username=username;
        this.account_id=account_id;
    }
    public void setThreshold(double moneyThreshold) {
        money=moneyThreshold;
    }
    public void setReminderMode(int mode) {
        this.mode=mode;
    }
    public boolean isSatisfied(IDataSource data) {
        IUser user=data.loadUser(username);
        IAccount account=Utilities.searchForAccount(user,account_id);
        if(account==null)
            return false;
        if(account.getTotalLeftMoney()<money) {
            remindingBody="your account：\n <<"+account.getAccountInfo().getValue("帐户名称").getString()
                    +">> have left money below"+money+"\r\n";
            return true;
        }else 
            return false;
    }

    public void conduct() {
        if(mode==RemainReminder.WINDOW_REMIND) {
            JFrame w=new JFrame(remindingTile);
            w.add(new JLabel(remindingBody));
            w.setLocationRelativeTo(null);
            w.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            w.setSize(400,300);
            w.setVisible(true);
        }else if(mode==RemainReminder.EMAIL_REMIND) {
            if(!Utilities.sendEmail(username, remindingTile, remindingBody)) {
                JFrame w=new JFrame(remindingTile);
                w.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                w.add(new JLabel(remindingBody+"PS:您的email提醒由于网络原因不能正常发送,请检查网络连接\r\n"));
                w.setSize(400,300);
                w.setLocationRelativeTo(null);
                w.setVisible(true);
            }
        }
    }

}
